package com.cuitrip.business.home.travel.ui;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.cuitrip.business.home.travel.model.FilterInfo;
import com.cuitrip.business.tripservice.ui.IconTextView;
import com.cuitrip.service.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sheena on 16/1/5.
 */
public class TravelTripFilterAdapter extends BaseAdapter{
    private ListView listView;
    private List<FilterInfo.ValueInfo> valueInfoList = new ArrayList<>();

    public TravelTripFilterAdapter(ListView listView, List<FilterInfo.ValueInfo> valueInfoList) {
        this.listView = listView;
        this.valueInfoList = valueInfoList;
    }


    @Override
    public int getCount() {
        return valueInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return valueInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if(convertView==null){
            convertView=LayoutInflater.from(parent.getContext()).inflate(R.layout.item_trip_filter_list,parent,false);
            viewHolder = new ViewHolder();
            convertView.setTag(viewHolder);
            viewHolder.text= (TextView) convertView.findViewById(R.id.text);
            viewHolder.icon= (IconTextView) convertView.findViewById(R.id.icon);
            viewHolder.divider1 = convertView.findViewById(R.id.divider1);
            viewHolder.divider2 = convertView.findViewById(R.id.divider2);
        }else{
            viewHolder= (ViewHolder) convertView.getTag();
        }
        if(position==(getCount()-1)){
            viewHolder.divider1.setVisibility(View.GONE);
            viewHolder.divider2.setVisibility(View.VISIBLE);
        }else {
            viewHolder.divider1.setVisibility(View.VISIBLE);
            viewHolder.divider2.setVisibility(View.GONE);
        }
        viewHolder.text.setText(valueInfoList.get(position).getKey());
        updateSelectStatus(position,viewHolder.icon);
        if (listView.getChoiceMode() == ListView.CHOICE_MODE_SINGLE){
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listView.setItemChecked(position,!listView.isItemChecked(position));
                }
            });
        }

        return convertView;
    }

    private void updateSelectStatus(int position,IconTextView icon) {
       icon.setVisibility( listView.isItemChecked(position)? View.VISIBLE:View.GONE);

    }

    static class ViewHolder{
        TextView text;
        IconTextView icon;
        View divider1;
        View divider2;
    }
}
